<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BMessageManager
 *
 * @author I062261
 */
class BSMessageManager {

    //put your code here
    public $owner;
    //put your code here
    protected $messageList = array();
    private $loaded = false;

    public function __construct($owner) {
        if ($owner instanceof BEntity)
            $this->owner = $owner;
    }

    public function addMessage($message, $data = NULL) {
        return $this->saveMessage($message);
    }

    public function removeMessage($message) {

        if ($message instanceof BSMessage) {
            foreach ($this->messageList as $name => $aMessage) {
                if ($aMessage instanceof BSMessage) {
                    if ($aMessage->getId() == $message->getId()) {
                        unset($this->messageList[$message->getId()]);
                    }
                }
            }
        }

        if (is_string($message)) {
            foreach ($this->messageList as $id => $aMessage) {
                if ($aMessage->getName() == $message)
                    unset($this->messageList[$id]);
            }
        }
    }

    public function getMessage($identity) {
        if (is_numeric($identity)) {
            foreach ($this->messageList as $id => $message) {
                if ($id == $identity)
                    return $message;
            }
        }
        if (is_string($identity)) {
            $name = $identity;
            foreach ($this->messageList as $message) {
                if ($message->getName() == $name)
                    return $message;
            }
        }
        return FALSE;
    }

    public function getMessageList($data = NULL) {
        if (!$this->loaded)
            $this->loadMessageList();
        return $this->messageList;
    }

    public function setMessageList($messageList) {
        if (is_array($messageList))
            $this->messageList = $messageList;
    }

    /**
     * this function save message into database
     * @param type $data
     */
    public function saveMessageList($data = NULL) {
        if ($this->owner instanceof BSEntity) {
            foreach ($this->messageList as $id => $message) {
                $this->saveMessage($message);
            }
        }
        return FALSE;
    }

    public function saveMessage($message) {
        $attributes = array();
        $attributes['resource_id'] = $this->owner->getId();
        $attributes['resource_type'] = get_class($this->owner);
        //$attributes['message_id'] = $id;
        $findResourceMessageList = BSMResourceMessage::model()->findAllByAttributes($attributes);
        foreach ($findResourceMessageList as $findResourceMessage) {
            if ($findResourceMessage instanceof CActiveRecord) {
                //if message saved already, just modify message
                $messageId = $findResourceMessage->message_id;
                //once fine exist, just save it
                if ($message instanceof BSMessage) {
                    if ($message->getId() == $messageId) {
                        $message->save();
                        return TRUE;
                    }
                }
            }
        }
        //else { //if not find in database, save message first and then add record into resourcemessage table
        if ($message instanceof BSMessage) {
            $message->save();
            $id = $message->getId();
            $row = new BSMResourceMessage();
            $row->resource_id = $this->owner->getId();
            $row->message_id = $id;
            $row->resource_type = get_class($this->owner);
            $row->save();
        }
        // }
    }

    /**
     * this function load messagelist from database
     * @param type $data
     */
    public function loadMessageList($data = NULL) {
        $attributes = array();
        $attributes['resource_id'] = $this->owner->getId();
        $attributes['resource_type'] = get_class($this->owner);
        $rowList = BSMResourceMessage::model()->findAllByAttributes($attributes);
        foreach ($rowList as $row) {
            $messageId = $row->message_id;
            $this->messageList[$messageId] = new BSMessage($messageId);
        }
        $this->loaded = TRUE;
    }

}

?>
